package com.redoor.app.stake.basicdata.controller;

import com.aliyun.openservices.ons.api.Action;
import com.aliyun.openservices.ons.api.ConsumeContext;
import com.aliyun.openservices.ons.api.Message;
import com.redoor.app.stake.basicdata.commons.BasicDataStatus;
import com.redoor.app.stake.basicdata.mq.consumerService.ChargePoleVersionMessageListener;
import com.redoor.app.stake.basicdata.mq.consumerService.UpdateModelMessageListener;
import com.redoor.app.stake.basicdata.mq.consumerService.UpdateOrganizationMessageListener;
import com.redoornetwork.framework.core.response.ResponseResult;
import com.redoornetwork.framework.core.response.ResultGenerator;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 *  mq的测试方法
 *
 * @author ZhangMumu  
 * @date 2018/4/11 0011 18:18  
 */
@RestController
@RequestMapping("/test/TestMq")
public class TsetMqController {
    @Autowired
    UpdateOrganizationMessageListener updateOrganizationMessageListener;

    @Autowired
    UpdateModelMessageListener updateModelMessageListener;

    @Autowired
    ChargePoleVersionMessageListener chargePoleVersionMessageListener;

    /**  
     *  mq同步组织机构的 测试
     *   
     * @author ZhangMumu  
     * @date 2018/4/11 0011 19:31
     * @param   
     * @return   
     */  
    @ApiOperation(value = "接收mq消息同步组织机构的测试方法")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "body", value = "mq的messageBody:ouCode编码", paramType = "query", dataType = "string", required = true, defaultValue = "2001376"),
            @ApiImplicitParam(name = "tag", value = "mq的tag:update||create||delete", paramType = "query", dataType = "string", required = true, defaultValue = "update")
    })
    @PostMapping("/testUpdateOrg")
    public ResponseResult testUpdateOrg(String body, String tag){
        Message message = new Message();
        message.setBody(body.getBytes());
        message.setTag(tag);
        message.setTopic("qfjs_sys_org");
        Action result = updateOrganizationMessageListener.consume(message, new ConsumeContext());
        if (Action.CommitMessage == result){
            return ResultGenerator.generatorSuccess();
        }else {
            return ResultGenerator.generatorFail(BasicDataStatus.OTHER_EXCEPTION,"mq测试失败");
        }
    }
    /**  
     *  mq同步计费模型的 测试
     *   
     * @author ZhangMumu  
     * @date 2018/4/11 0011 19:41  
     * @param
     * @return
     */
    @ApiOperation(value = "接收mq消息同步计费模型的测试方法(后台需要调用接口,本地无法测试)")
    @ApiImplicitParam(name = "body", value = "mq的messageBody", paramType = "query", dataType = "string", required = true, defaultValue = "{\"chargePort\":\"0\",\"id\":1184254,\"limitCount\":1,\"modelId\":\"1110116072690002\",\"relTime\":\"2017-06-29T14:35:33\",\"serverType\":\"02\",\"stakeNo\":\"1140290000001913\"}")
    @PostMapping("/testUpdateFeeModel")
    public ResponseResult testUpdateFeeModel(String body){
        Message message = new Message();
        message.setBody(body.getBytes());
        message.setTopic("pc_model_stake_rel");
        message.setTopic("PcModelStakeRel");
        Action result = updateModelMessageListener.consume(message, new ConsumeContext());
        if (Action.CommitMessage == result){
            return ResultGenerator.generatorSuccess();
        }else {
            return ResultGenerator.generatorFail(BasicDataStatus.OTHER_EXCEPTION,"mq测试失败");
        }
    }
    /**  
     * mq更新枪版本号测试
     *   
     * @author ZhangMumu  
     * @date 2018/4/11 0011 19:51  
     * @param   
     * @return   
     */  
    @ApiOperation(value = "更新枪版本号的mq 测试")
    @ApiImplicitParam(name = "body", value = "mq的messageBody", paramType = "query", dataType = "string", required = true, defaultValue = "{\"chargingPileNum\":\"3140190000002599\",\"masterVersion\":2,\"slaveVersion\":44}")
    @PostMapping("/testChargePoleVersion")
    public ResponseResult testChargePoleVersion(String body){
        Message message = new Message();
        message.setBody(body.getBytes());
        message.setTopic("charger_mangement_mqup");
        message.setTag("A63_1");
        Action result = chargePoleVersionMessageListener.consume(message, new ConsumeContext());
        if (Action.CommitMessage == result){
            return ResultGenerator.generatorSuccess();
        }else {
            return ResultGenerator.generatorFail(BasicDataStatus.OTHER_EXCEPTION,"mq测试失败");
        }
    }
}
